import VueRouter from "vue-router";
import Vue from 'vue'
import userLoginAndRegister from '@/views/UserLoginAndRegister/userLoginAndRegister'
import tapLayout from '@/views/TapLayout/tapLayout'
import userInfo from '@/views/UserInfo/userInfo'
import collegeInfo from '@/views/CollegeInfo/collegeInfo'
import pressInfo from '@/views/PressInfo/pressInfo'
import homePage from '@/views/HomePage/homePage'
import tsManager from '@/views/TsManager/tsManager'
import teacherInfo from '@/views/TsManager/TeacherInfo/teacherInfo'
import studentInfo from '@/views/TsManager/StudentInfo/studentInfo'
import thesisManager from '@/views/ThesisManager/thesisManager'
import thesisList from '@/views/ThesisManager/ThesisList/thesisList'
import thesisExam from '@/views/ThesisManager/ThesisExam/thesisExam'
import personThesis from '@/views/ThesisManager/PersonThesis/personThesis'
import selfInfo from '@/views/SelfInfo/selfInfo'

Vue.use(VueRouter)
const routes = [
  {
    path: "/",
    redirect: "/login",
    component: userLoginAndRegister,
  },
  {
    name: "登录",
    path: "/login",
    component: userLoginAndRegister
  },
  {
    name: "布局",
    path: "/layout",
    redirect: "/home/page",
    component: tapLayout,
    children: [
      {
        name: "主页",
        path: "/home/page",
        component: homePage
      },
      {
        name: "用户信息",
        path: "/user/info",
        component: userInfo
      },
      {
        name: "学院信息",
        path: "/college/info",
        component: collegeInfo
      },
      {
        name: "出版社信息",
        path: "/press/info",
        component: pressInfo
      },
      {
        name: "师生信息查询",
        path: "/ts",
        component: tsManager,
        children: [
          {
            name: "教师信息查询",
            path: "/ts/teacher/info",
            component: teacherInfo
          },
          {
            name: "学生信息查询",
            path: "/ts/student/info",
            component: studentInfo
          }
        ]
      },
      {
        name: "论文管理",
        path: "/thesis",
        component: thesisManager,
        children: [
          {
            name: "论文查询",
            path: "/thesis/list",
            component: thesisList
          },
          {
            name: "个人论文管理",
            path: "/thesis/person",
            component: personThesis
          },
          {
            name: "论文审批",
            path: "/thesis/exam",
            component: thesisExam
          }
        ]
      }
    ]
  },
  {
    name: '个人信息',
    path: '/self/info',
    component: selfInfo
  }
]

//路由导航守卫!!!!!!!

//路由对象的定义
const router = new VueRouter({
  routes
})


/**
 * 路由导航守卫的实现
 * 说明：每次请求在访问目标网址之前都会被拦截
 * 参数：
 *  1.to 用户请求的网址对象
 *  2.from 用户请求的来源
 *  3.next 请求的放行
 */

 router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  //2.判断是否有token
  let userInfo = localStorage.getItem('userInfo');
  if (userInfo !== null && userInfo !== '') {
    return next()
  }
  //3.如果数据为空
  next('/login')
})

export default router;
